package io.github.maojian.str;

import org.junit.Test;

import java.util.Arrays;

/**
 * 899. 有序队列
 */
public class Q899 {
    public String orderlyQueue(String s, int k) {
        if (k > 1) {
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            return new String(chars);
        } else {
            String smallest = s;
            StringBuilder stringBuilder = new StringBuilder(s);
            int length = s.length();
            for (int i = 1; i < length; i++) {
                char c = stringBuilder.charAt(0);
                stringBuilder.deleteCharAt(0);
                stringBuilder.append(c);
                if (stringBuilder.toString().compareTo(smallest) < 0) {
                    smallest = stringBuilder.toString();
                }
            }
            return smallest;
        }
    }

    @Test
    public void test() {
        String s = "baaca";
        int k = 1;
        System.out.println(orderlyQueue(s, k));
    }
}
